<?php

/**
 * 订单操作类 sqlHandler
 * @package  htdocs
 * @subpackage  quanliadmin/app/project
 * @create by allen.qiang
 * @version 1.0
 */

/**
 * Class sqlHandler
 *  searchProjectList  //获取项目信息
 *  getProjectDataBase //获取项目数据库信息
 *  getSqlList         //获取项目发布的SQL信息
 *  issueSql           //发布SQL
 *  getSqlPubDetail    //获取项目在其各个数据库中SQL的执行结果信息
 *  executeSql         //在单个数据库中执行SQL
 */

class sqlHandler{
    private $dbca;
    private $dateTime;
    private $currentDate;

    function __construct(&$dbca){
        $this->dbca = $dbca;
        $this->dateTime = date('Y-m-d H:i:s',time());
        $this->currentDate = date('Y-m-d');
    }

    /**
     * 获取项目信息
     * $arg_array = array (
                            'projectID' => int 项目ID
                            'sortname' => string 排序字段
                            'sortorder' => string 排序方式
                            'page' => int 分页页码
                            'rp' => int 分页页数
                            )
     */
    public function searchProjectList( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //项目ID
        if ( isset($arg_array['projectID']) && is_numeric($arg_array['projectID']) ) {
            $sql .= ' AND projectID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['projectID'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_project ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询项目信息
            $search_sql = 'SELECT * FROM sys_project ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'  => $item->creator,
                    'timeStamp'=> $item->timeStamp,
                    'createTime' => $item->createTime,
                    'projectID' => $item->projectID,
                    'projectName' => $item->projectName,
                    'extend_1' => $item->extend_1,
                    'extend_2' => $item->extend_2
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;


        }

    }


    /**
     * 获取项目数据库信息
     * $arg_array = array (
                            'projectID' => int 项目ID
                            'sortname' => string 排序字段
                            'sortorder' => string 排序方式
                            'page' => int 分页页码
                            'rp' => int 分页页数
                            )
     */
    public function getProjectDataBase( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //项目ID
        if ( isset($arg_array['projectID']) && is_numeric($arg_array['projectID']) ) {
            $sql .= ' AND projectID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['projectID'];
        }

        //数据库ID
        if ( isset($arg_array['dbID']) && is_numeric($arg_array['dbID']) ) {
            $sql .= ' AND dbID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['dbID'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_project_db ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询项目数据库信息
            $search_sql = 'SELECT * FROM sys_project_db ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'  => $item->creator,
                    'timeStamp'=> $item->timeStamp,
                    'createTime' => $item->createTime,
                    'projectID'=>$item->projectID,
                    'projectName'=>$item->projectName,
                    'dbID'=>$item->dbID,
                    'dbname'=>$item->dbname,
                    'db_byname'=>$item->db_byname,
                    'host'=>$item->host,
                    'port'=>$item->port,
                    'userName'=>$item->userName,
                    'userPwd'=>$item->userPwd,
                    'servicename'=>$item->servicename,
                    'dbflg'=>$item->dbflg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    /**
     * 获取项目发布的SQL信息
     * $arg_array 查询参数数组
     */
    public function getSqlList( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //流水号
        if (isset($arg_array['seqno']) && is_numeric($arg_array['seqno'])) {
            $sql .= ' AND seqno = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['seqno'];
        }

        //项目ID
        if (isset($arg_array['projectID']) &&  !empty($arg_array['projectID']) ) {
            $sql .= ' AND projectID = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['projectID']);
        }

        //创建人
        if (isset($arg_array['creator']) &&  !empty($arg_array['creator']) ) {
            $sql .= ' AND creator = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['creator']);
        }

        //操作表名
        if (isset($arg_array['tableName']) &&  !empty($arg_array['tableName']) ) {
            $sql .= ' AND sqltxt LIKE ?';
            $params_type .= 's';
            $params_array[] = '%'.trim($arg_array['tableName']).'%';
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_project_db_sql ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询项目发布的SQL信息
            $search_sql = 'SELECT * FROM sys_project_db_sql ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'  => $item->creator,
                    'timeStamp'=> $item->timeStamp,
                    'createTime' => $item->createTime,
                    'seqno'=>$item->seqno,
                    'projectID'=>$item->projectID,
                    'projectName'=>$item->projectName,
                    'sqltxt'=>$item->sqltxt,
                    'memo'=>$item->memo
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 发布SQL
     * @param array $arg_array 参数数组
     * $arg_array = array(
                            'projectID'=>1                 //项目ID
                            'checkedArr'=>array(1,2,3,4)   //选中的要发布SQL的数据库ID序列(dbID序列)
                            'sqltxt'=>'select * from test' //发布的SQL语句
                            'creator'=>'zhoulaiyin'        //当前发布人
                            'memo'=>'测试'                 //备注信息
                         )
     *
     * @return array
     * @throws Exception
     */
    public function issueSql( $arg_array ) {

        //检查参数是否完整
        if( !isset($arg_array['projectID'] , $arg_array['checkedArr'] , $arg_array['sqltxt'] , $arg_array['creator']) ){
            return array( 'code'=>400 , 'message'=>'参数错误，请与管理员联系' );
        }

        //接收参数
        $projectID  = $arg_array['projectID'];
        $checkedArr = $arg_array['checkedArr'];
        $sqltxt     = htmlspecialchars( trim($arg_array['sqltxt']) );
        $creator    = $arg_array['creator'];
        $memo       = isset($arg_array['memo']) ? htmlspecialchars( trim($arg_array['memo']) ) : '';

        //连接错误信息的字符串
        $error_msg = '';

        if(empty($projectID))
            $error_msg .= '项目不能为空<br>';

        if(empty($checkedArr))
            $error_msg .= '请选择发布的数据库<br>';

        if(empty($sqltxt))
            $error_msg .= '请填写发布的SQL<br>';

        if( empty($creator) )
            $error_msg .= '当前发布人不能为空<br>';


        if( $error_msg != '' )
            return array( 'code'=>400 , 'message'=>$error_msg );

        $seqno = isset($arg_array['seqno']) ? trim($arg_array['seqno']) : null;

        //查询项目信息
        $projectInfo = $this->searchProjectList( $arg_array );


        try{

            //事务开启
            $this->dbca->beginTransaction();

            $sqlObj = new entity('sys_project_db_sql');

            if( $seqno ){

                $sqlObj->seqno = $seqno;
                if( !$this->dbca->fetch($sqlObj) )
                    throw new Exception( '没有找到信息' , 404 );

            }

            $sqlObj->creator     = $creator;
            $sqlObj->sqltxt      = $sqltxt;
            $sqlObj->projectID   = $projectID;
            $sqlObj->projectName = $projectInfo[0]['projectName'];
            $sqlObj->memo        = $memo;

            //保存发布的SQL
            $this->dbca->save($sqlObj);

            //将SQL按“;”拆分成数组
            $sqlArr = explode( ';' , $sqltxt );

            if( empty($seqno) )
                $seqno = $this->dbca->insert_id;  //获取最新的自增量值

            //获取该项目库中所有的数据库
            $databaseArr = $this->getProjectDataBase( $arg_array );

            /**
             * 循环检查哪些数据库是需要执行SQL
             * $databaseArr  =>  项目下所有的数据库
             * $checkedArr   =>  页面选中的要执行SQL的数据库
             *
             */
            foreach ( $databaseArr as $dbObj ){

                $sqlPubObj = new entity('sys_project_sql_pub');
                $sqlPubObj->creator = $creator;
                $sqlPubObj->seqno   = $seqno;
                $sqlPubObj->dbID    = $dbObj['dbID'];
                $sqlPubObj->dbname  = $dbObj['dbname'];

                if( in_array( $dbObj['dbID'] , $checkedArr ) ){
                    $checkedFlg = 1;
                }else{
                    $checkedFlg = 0;
                }

                if( $checkedFlg == 0 ){

                    $sqlPubObj->msgtext   = NULL;
                    $sqlPubObj->flg       = $checkedFlg;
                    $this->dbca->save($sqlPubObj);

                    continue;

                }

                //连接数据库的错误信息
                $error_message = '';

                //记录SQL执行结果信息数组
                $msgArr = array();

                /**
                 * dbflg:数据库类型   0：MYSQL  1:ORACLE
                 */
                if ( $dbObj['dbflg'] == 0 ){

                    //连接MYSQL数据库，执行SQL
                    $conn = mysqli_connect($dbObj['host'] . ':' . $dbObj['port'],$dbObj['userName'],$dbObj['userPwd'],$dbObj['dbname']);

                    if(!$conn){
                        $error_message .= '无法连接到('.$dbObj['host'].'-'.$dbObj['dbname'].')MYSQL数据库:' . mysqli_connect_error();
                        $msgArr[] = mysqli_connect_error();
                        $flg = 0;
                    }else{
                        $flg = 1;

                        //循环执行SQL
                        foreach( $sqlArr as $sql ){

                            if( empty($sql) )
                                continue;

                            $result = mysqli_query( $conn , $sql );  //执行SQL语句

                            $errorReturn = mysqli_error($conn);

                            if( !empty($errorReturn) && !in_array( $errorReturn , $msgArr ) )
                                $msgArr[] = $errorReturn; //获取执行SQL的结果信息

                        }

                        mysqli_close($conn); //关闭数据库连接
                    }

                }else{

                    //连接ORACLE数据库,执行sql
                    $dbstr = '(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =' . $dbObj['host'] . ')(PORT = ' . $dbObj['port'] . '))(CONNECT_DATA =(SERVICE_NAME = ' . $dbObj['servicename'] . ')))';
                    $oracle_conn = oci_connect($dbObj['userName'],$dbObj['userPwd'], $dbstr, 'AL32UTF8');

                    if(!$oracle_conn){
                        $flg = 0;
                        $error = oci_error();
                        $error_message .= '无法连接到('.$dbObj['host'].'-'.$dbObj['dbname'].')ORACLE数据库:' . $error['message'];
                    }else{

                        foreach( $sqlArr as $sql ){

                            if( empty($sql) )
                                continue;

                            $stmt = oci_parse( $oracle_conn, $sql );  //执行SQL语句
                            $remark_1 = oci_error($oracle_conn);
                            oci_execute($stmt);
                            $error_array = oci_error($stmt);
                            $msgArr[] = $remark_1['message'] . '，'.$error_array['message'];  //获取执行SQL的结果信息

                        }

                        oci_free_statement($stmt);
                        oci_close($oracle_conn);  //关闭数据库连接
                        $flg = 1;

                    }

                }

                if( $msgArr )
                    $msgtext = implode( '<br>' , $msgArr );
                else
                    $msgtext = 'ok';

                $sqlPubObj->msgtext   = $msgtext;
                $sqlPubObj->flg       = $flg;

                if( $error_message != '' )
                    throw new Exception( $error_message , 400 );

                $this->dbca->save($sqlPubObj);

            }

            //提交事务
            $this->dbca->commit();

            return array( 'code'=>200 , 'message'=>'发布成功');

        }catch(Exception $e){

            //回滚事务
            $this->dbca->rollBack();

            if( $e->getCode() > 0 ){

                return array( 'code'=>$e->getCode() , 'message'=>$e->getMessage() );

            }else{

                error_log($e->getMessage());
                return array( 'code'=>500 , 'message'=>'系统错误，操作失败' );

            }

        }

    }

    /**
     * 获取项目发布的SQL的执行结果信息
     * $arg_array 查询参数数组
     */
    public function getSqlPubDetail( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //数据库ID
        if (isset($arg_array['dbID']) && is_numeric($arg_array['dbID'])) {
            $sql .= ' AND s1.dbID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['dbID'];
        }

        //流水号
        if (isset($arg_array['seqno']) &&  !empty($arg_array['seqno']) ) {
            $sql .= ' AND s1.seqno = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['seqno']);
        }

        //创建人
        if (isset($arg_array['flg']) &&  in_array( $arg_array['flg'] , array(0,1) ) ) {
            $sql .= ' AND s2.flg = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['flg']);
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_project_db_sql s1 INNER JOIN sys_project_sql_pub s2 ON s1.seqno = s2.seqno ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询SQL在项目每个数据库中的执行情况
            $search_sql = 'SELECT s1.seqno,s1.creator,s1.createTime,s1.projectID,s1.projectName,s1.sqltxt,s1.memo,
                        s2.dbID,s2.dbname,s2.flg,s2.msgtext
                        FROM sys_project_db_sql s1 INNER JOIN sys_project_sql_pub s2 ON s1.seqno = s2.seqno ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'  => $item->creator,
                    'createTime' => $item->createTime,
                    'seqno'=>$item->seqno,
                    'projectID'=>$item->projectID,
                    'projectName'=>$item->projectName,
                    'sqltxt'=>$item->sqltxt,
                    'memo'=>$item->memo,
                    'dbID'=>$item->dbID,
                    'dbname'=>$item->dbname,
                    'msgtext'=>$item->msgtext,
                    'flg'=>$item->flg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 在单个数据库中执行SQL
     * @params $arg_array  参数数组
     */
    function executeSql( $arg_array ){

        //检查参数
        if( !isset( $arg_array['seqno'] , $arg_array['dbID'] ) )
            return array( 'code'=>400 , 'message'=>'参数错误' );

        //接收参数
        $seqno = $arg_array['seqno'];
        $dbID  = $arg_array['dbID'];

        if( empty($seqno) || empty($dbID) )
            return array( 'code'=>400 , 'message'=>'参数错误' );

        //查询信息
        $sqlPub_obj = new entity('sys_project_sql_pub');
        $sqlPub_obj->seqno = $seqno;
        $sqlPub_obj->dbID = $dbID;

        if( !$this->dbca->fetch($sqlPub_obj) )
            return array( 'code'=>404 , 'message'=>'信息没找到' );

        //查询发布的序号信息
        $pubInfo = $this->getSqlList( $arg_array );

        if( empty($pubInfo) )
            return array( 'code'=>404 , 'message'=>'没有找到该发布版本信息' );

        //获取执行的SQL
        $sqltxt = $pubInfo[0]['sqltxt'];

        //查询数据库信息
        $dbInfo = $this->getProjectDataBase( $arg_array );
        if( empty($dbInfo) )
            return array( 'code'=>404 , 'message'=>'没有找到该数据库信息' );

        //将SQL按“;”拆分成数组
        $sqlArr = explode( ';' , $sqltxt );

        //获取该数据库信息
        $databaseInfo = $dbInfo[0];

        //连接数据库SQL执行结果信息
        $error_message = '';

        //记录SQL执行结果信息数组
        $msgArr = array();

        /**
         * dbflg:数据库类型 0：mysql  1:oracle
         */
        if ( $databaseInfo['dbflg'] == 0 ){

            //连接MYSQL数据库，执行SQL
            $conn = mysqli_connect($databaseInfo['host'] . ':' . $databaseInfo['port'],$databaseInfo['userName'],$databaseInfo['userPwd'],$databaseInfo['dbname']);

            if(!$conn){
                $error_message .= '无法连接到('.$databaseInfo['host'].'-'.$databaseInfo['dbname'].')MYSQL数据库:' . mysqli_connect_error();
                $msgArr[] = mysqli_connect_error();
                $flg = 0;
            }else{
                $flg = 1;

                //循环执行SQL语句
                foreach( $sqlArr as $sql ){

                    if( empty($sql) )
                        continue;

                    $result = mysqli_query( $conn , $sql );  //执行SQL

                    //接收SQL执行结果
                    $errorReturn = mysqli_error($conn);

                    if( !empty($errorReturn) && !in_array( $errorReturn , $msgArr ) )
                        $msgArr[] = $errorReturn; //获取执行SQL的结果信息

                }

                mysqli_close($conn);                      //关闭数据库连接
            }

        }else{

            //连接ORACLE数据库,执行sql
            $dbstr = '(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST =' . $databaseInfo['host'] . ')(PORT = ' . $databaseInfo['port'] . '))(CONNECT_DATA =(SERVICE_NAME = ' . $databaseInfo['servicename'] . ')))';
            $oracle_conn = oci_connect($databaseInfo['userName'],$databaseInfo['userPwd'], $dbstr, 'AL32UTF8');

            if(!$oracle_conn){
                $flg = 0;
                $error = oci_error();
                $error_message .= '无法连接到('.$databaseInfo['host'].'-'.$databaseInfo['dbname'].')ORACLE数据库:' . $error['message'];
            }else{

                foreach( $sqlArr as $sql ){

                    if( empty($sql) )
                        continue;

                    $stmt = oci_parse( $oracle_conn, $sqltxt );  //执行SQL
                    $remark_1 = oci_error($oracle_conn);
                    oci_execute($stmt);
                    $error_array = oci_error($stmt);
                    $msgArr[] = $remark_1['message'] . '，'.$error_array['message'];  //获取执行SQL结果

                }

                oci_free_statement($stmt);
                oci_close($oracle_conn);  //关闭数据库连接
                $flg = 1;

            }

        }

        if( $error_message != '' )
            return array( 'code'=>400 , 'message'=>$error_message );



        try{

            if( $msgArr )
                $msgtext = implode( '<br>' , $msgArr );
            else
                $msgtext = 'ok';

            $sqlPub_obj->flg = $flg;
            $sqlPub_obj->msgtext = $msgtext;

            $this->dbca->save( $sqlPub_obj );

            return array( 'code'=>200 , 'message'=>'发布成功' );

        }catch (Exception $e){

            error_log( $e->getMessage() );
            return array( 'code'=>500 , 'message'=>'系统错误，发布失败' );

        }

    }


}
